//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package org.workplicity.android.cia.ui.elogfragments;

import java.util.Calendar;

import org.workplicity.android.cia.R;
import org.workplicity.android.cia.ui.ElogEditActivity;
import org.workplicity.android.cia.util.CIAHelper;
import org.workplicity.elog.entry.ElogUser;
import org.workplicity.elog.entry.Audit;

import android.app.ListFragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * 
 * @author brian.gormanly
 *
 */

public class CatchEditFragment extends ListFragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View v = inflater.inflate(R.layout.catch_edit_fragment, container, false);
		
		populateTitles();
		
		return v;
	}
	
	
	public void populateTitles() {

    	ElogEditActivity myActivity = (ElogEditActivity)getActivity();
    	
    	// intialize array of choices
    	String[] choices;
    	
    	if(myActivity.getApp().getCurrentElog().getAuditTrail().size() > 0) {
    		choices = new String[myActivity.getApp().getCurrentElog().getAuditTrail().size()];
    	}
    	else {
    		choices = new String[1];
    	}
    	
    	// get the resourcess available
        Resources res = getResources();

    	// worklogs
    	// Get the string array that has the tabs / names
        //choices = res.getStringArray(R.array.workorders_array);

    	choices[0] = "Loading...";
    	
    	Audit audit = null;
		Boolean[] isRecent = new Boolean[myActivity.getApp().getCurrentElog().getAuditTrail().size()];
    	
    	for(int i=0; i<myActivity.getApp().getCurrentElog().getAuditTrail().size(); i++) {
    		audit = myActivity.getApp().getCurrentElog().getAuditTrail().get(i);
    		
    		Calendar cal = Calendar.getInstance();
    		cal.setTimeInMillis(audit.getDate().getTime());
    		
    		ElogUser thisUser = myActivity.getApp().getUserById(audit.getUserId());
		
    		choices[i] = "Updated: " + CIAHelper.asDate(cal) + " " + CIAHelper.asTime(audit.getDate()) + "\n" +
    			"By: " + thisUser.getFirstName().toString() + " " + thisUser.getLastName().toString() + "\n" +
    			"Status: " + audit.getText();
    		
        	isRecent[i] = CIAHelper.isRecent(audit.getDate());

    	}
            

    	// set list adapter will go here
    	setListAdapter(new MyAdapter(myActivity, choices, isRecent));
        
    }
	
private class MyAdapter extends ArrayAdapter<String> {
    	
    	String[] names;
    	Boolean[] isRecent;
    	 
        public MyAdapter(Context context, String[] names, Boolean[] isRecent) {
            super(context, R.layout.title_list_item, R.id.listResource, names);
            
            this.names = names;
            if(isRecent != null) {
            	this.isRecent = isRecent;
            }
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            
        	LayoutInflater mInflater = (LayoutInflater)getActivity().getLayoutInflater();
        	
        	View row;
        	 
        	if (null == convertView) {
        		row = mInflater.inflate(R.layout.title_list_item, null);
        	} else {
        		row = convertView;
        	}
        	
        	//TextView tv = (TextView) row.findViewById(android.R.id.text1);
        	TextView tv = (TextView)super.getView(position, convertView, parent);

        	tv.setText(names[position]);

        	return tv;
        }
    }
	
	
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
    
    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
		// this will do something
    	
	}
}
